package com.diorsunion.hedge.client.ui;

import com.diorsunion.hedge.borker.BorkerClient;
import com.diorsunion.hedge.common.ConnectionStatus;
import com.diorsunion.hedge.common.ServiceResult;
import com.diorsunion.hedge.io.IOClient;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.swing.*;
import java.awt.*;
import java.net.InetSocketAddress;

/**
 * 按钮面板，大部分按钮的逻辑都写在这里
 * Created by harley-dog on 2016/5/23.
 */
@org.springframework.stereotype.Component
public class ButtonPanel extends JPanel implements UI {
    @Resource
    @Qualifier("ClientFrame")
    JFrame jFrame;
    @Resource
    @Qualifier("infoLogPanel")
    private TextPanel infoLogPanel;
    @Resource
    @Qualifier("tradeLogPanel")
    private TextPanel tradeLogPanel;
    @Resource
    @Qualifier("errorLogPanel")
    private TextPanel errorLogPanel;
    ConnectDlg ibConnectDlg = new ConnectDlg("连接券商客户端", "127.0.0.1", 7497, x -> doConnectBorker(x));
    ConnectDlg nettyConnectDlg = new ConnectDlg("连接Hedge服务端", "127.0.0.1",9080, x -> onConnectServer(x));

    public ButtonPanel(){
        this.setLayout(new GridLayout(10, 1));
        JButton butConnectBroker = new JButton("连接券商客户端");
        butConnectBroker.addActionListener(x -> onConnect(ibConnectDlg));
        this.add(butConnectBroker);
        JButton butDisConnectBroker = new JButton("断开券商连接");
        butDisConnectBroker.addActionListener(x -> disConnectBorker());
        this.add(butDisConnectBroker);
        JButton butCheckBrokerStatus = new JButton("查看券商状态");
        butCheckBrokerStatus.addActionListener(x -> checkBorkerStatus());
        this.add(butCheckBrokerStatus);
        this.add(new JSeparator ());
        JButton butConnectServer = new JButton("连接Hedge服务端");
        butConnectServer.addActionListener(x -> onConnect(nettyConnectDlg));
        this.add(butConnectServer);
        JButton butServerBrokerStatus = new JButton("查看Server状态");
        butServerBrokerStatus.addActionListener(x -> checkServerStatus());
        this.add(butServerBrokerStatus);
    }

    @PostConstruct
    @Override
    public void init() {
    }

    void onConnect(ConnectDlg dlg) {
        dlg.setVisible(true);
        if (!dlg.m_rc) {
            return;
        }
    }


    @Resource
    BorkerClient borkerClient;
    boolean doConnectBorker(InetSocketAddress socketAddress) {
        ServiceResult serviceResult = borkerClient.connect(socketAddress.getHostName(), socketAddress.getPort());//先连接客户端
//        ServiceResult serviceResult = ioClient.status();//再查询客户端的连接状态
        if (serviceResult.obj == ConnectionStatus.OK) {
            tradeLogPanel.add("连接"+borkerClient.getBorkerName()+"成功");
//            borkerClient
            return true;
        } else {
            errorLogPanel.add(serviceResult.message);
            return false;
        }
    }

    void disConnectBorker() {
        ServiceResult serviceResult = borkerClient.disConnect();
        if (serviceResult.obj == ConnectionStatus.NoOK) {
            tradeLogPanel.add("断开"+borkerClient.getBorkerName()+"成功");
        } else {
            tradeLogPanel.add("断开"+borkerClient.getBorkerName()+"失败");
        }
    }

    void checkBorkerStatus() {
        ServiceResult serviceResult = borkerClient.status();
        if (serviceResult.obj == ConnectionStatus.OK) {
            tradeLogPanel.add(borkerClient.getBorkerName()+"已经连接");
        } else {
            tradeLogPanel.add(borkerClient.getBorkerName()+"尚未连接");
        }
    }

    @Resource
    IOClient ioClient;
    boolean onConnectServer(InetSocketAddress socketAddress) {
        ioClient.connect(socketAddress.getHostName(), socketAddress.getPort());//先连接客户端
        ServiceResult serviceResult = ioClient.status();//再查询客户端的连接状态
        if (serviceResult.obj == ConnectionStatus.OK) {
            infoLogPanel.add(serviceResult.message);
            return true;
        } else {
            errorLogPanel.add(serviceResult.message);
            return false;
        }
    }

    void checkServerStatus() {
        ServiceResult serviceResult = ioClient.status();
        if (serviceResult.obj == ConnectionStatus.OK) {
            infoLogPanel.add("Server已经连接");
        } else {
            infoLogPanel.add("Server尚未连接");
        }
    }

}